package com.lanou.dllo.soccernews.fragment.circle2fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.adapter.HotAdapter;
import com.lanou.dllo.soccernews.bean.DataResult;
import com.lanou.dllo.soccernews.bean.HotBean;
import com.lanou.dllo.soccernews.volley.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/29.
 */
public class HotFragment extends Fragment {
    private ListView list;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private HotAdapter adapter;
    private Context context;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_hot, null);

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        list = (ListView) view.findViewById(R.id.hot_list);


    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        addDatas();

    }

    private void addDatas() {

        String url = "https://api.dongqiudi.com/recommend_topics?page=1";

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();

                HotBean hotBean = gson.fromJson(response, HotBean.class);

                if (hotBean != null) {
                    List<DataResult> dataResults = hotBean.getData();

                    if (dataResults != null && dataResults.size() > 0) {

                        //添加数据
                       // Log.i("lanopu",dataResults+"");

                        adapter = new HotAdapter(getActivity());

                        list.setAdapter(adapter);
                        adapter.addDatas(dataResults);
                    }

                }


            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);
    }
}
